/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sbmart.smartserver.service;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sbmart.object.*;
import sbmart.smartserver.cache.ConnectionManager;

/**
 *
 * @author albert
 */
public class ClientMstService {

    private CallableStatement call_put_mst_card;
    private CallableStatement call_put_mst_code;
    private CallableStatement call_put_mst_hadiah;
    private CallableStatement call_put_mst_item;
    private CallableStatement call_put_mst_item_discount;
    private CallableStatement call_put_mst_item_hpp;
    private CallableStatement call_put_mst_item_last_cost;
    private CallableStatement call_put_mst_item_display;
    private CallableStatement call_put_mst_item_price_tmp;
    private CallableStatement call_put_mst_item_price_active;
    private CallableStatement call_put_mst_item_price_promo;
    private CallableStatement call_put_mst_item_price_promo_active;
    private CallableStatement call_put_mst_item_vendor;
    private CallableStatement call_put_mst_master;
    private CallableStatement call_put_mst_modul_produk;
    private CallableStatement call_put_mst_rak_sewa;
    private CallableStatement call_put_mst_relation;
    private CallableStatement call_put_mst_rent_terrace;
    private CallableStatement call_put_mst_tenant;
    private CallableStatement call_put_mst_voucher;
    private CallableStatement call_put_trs_kirim_mst;
    private CallableStatement call_put_trs_kirim_detail;
    private CallableStatement call_put_trs_kirimp_mst;
    private CallableStatement call_put_trs_kirimp_detail;
    private CallableStatement call_put_mst_point_mapping;
    private CallableStatement call_put_mst_branch;
    private static ClientMstService instance;
    public static final Logger LOG = LoggerFactory.getLogger("ServerMst");
    private Object clientConn;

    private ClientMstService() {
    }

    public static ClientMstService instance() {
        if (instance == null) {
            instance = new ClientMstService();
            instance.init();
        }
        return instance;
    }

    public void init() {
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            LOG.debug("<ERROR>", ex);
        }
    }

    public Connection putMstMaster(Header header, List<MstMaster> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_master = clientConn.prepareCall(ClientProcedures.put_mst_master);
        for (MstMaster mst : data) {
            call_put_mst_master.setInt("mst_id", mst.getMstId());
            call_put_mst_master.setInt("mst_tipe", mst.getMstTipe());
            call_put_mst_master.setString("mst_code", mst.getMstCode());
            call_put_mst_master.setString("mst_name", mst.getMstName());
            call_put_mst_master.setString("edit_log", mst.getEditLog());
            call_put_mst_master.setString("insert_log", mst.getInsertLog());
            call_put_mst_master.setLong("version", mst.getVers());
            call_put_mst_master.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_master.executeBatch();
        return clientConn;
    }

    public Connection putMstCode(Header header, List<MstCode> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_code = clientConn.prepareCall(ClientProcedures.put_mst_code);
        for (MstCode mst : data) {
            call_put_mst_code.setInt("code_id", mst.getCodeId());
            call_put_mst_code.setString("code_code", mst.getCodeCode());
            call_put_mst_code.setString("code_name", mst.getCodeName());
            call_put_mst_code.setInt("parent_id", mst.getParentId());
            call_put_mst_code.setInt("level", mst.getLevel());
            call_put_mst_code.setInt("urut", mst.getUrut());
            call_put_mst_code.setString("edit_log", mst.getEditLog());
            call_put_mst_code.setString("insert_log", mst.getInsertLog());
            call_put_mst_code.setLong("version", mst.getVers());
            call_put_mst_code.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_code.executeBatch();
        return clientConn;
    }

    public Connection putMstHadiah(Header header, List<MstHadiah> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_hadiah = clientConn.prepareCall(ClientProcedures.put_mst_hadiah);
        for (MstHadiah mst : data) {
            call_put_mst_hadiah.setInt("hadiah_id", mst.getHadiahId());
            call_put_mst_hadiah.setString("item_code", mst.getItemCode());
            call_put_mst_hadiah.setString("item_name", mst.getItemName());
            call_put_mst_hadiah.setString("insert_log", mst.getInsertLog());
            call_put_mst_hadiah.setString("edit_log", mst.getEditLog());
            call_put_mst_hadiah.setLong("version", mst.getVers());
            call_put_mst_hadiah.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_hadiah.executeBatch();
        return clientConn;
    }

    public Connection putMstItem(Header header, List<MstItem> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item = clientConn.prepareCall(ClientProcedures.put_mst_item);
        for (MstItem mst : data) {
            call_put_mst_item.setInt("p_item_id", mst.getItemId());
            call_put_mst_item.setString("p_item_code", mst.getItemCode());
            call_put_mst_item.setString("p_barcode", mst.getBarcode());
            call_put_mst_item.setString("p_barcode2", mst.getBarcode2());
            call_put_mst_item.setString("p_barcode3", mst.getBarcode3());
            call_put_mst_item.setString("p_item_name", mst.getItemName());
            call_put_mst_item.setString("p_divisi_po", mst.getDivisiPo());
            call_put_mst_item.setString("p_struk_name", mst.getStrukName());
            call_put_mst_item.setShort("p_specification", mst.getSpecification());
            call_put_mst_item.setByte("p_bkp", mst.getBkp());
            call_put_mst_item.setByte("p_halal", mst.getHalal());
            call_put_mst_item.setShort("p_unit1", mst.getUnit1());
            call_put_mst_item.setShort("p_unit2", mst.getUnit2());
            call_put_mst_item.setShort("p_unit3", mst.getUnit3());
            call_put_mst_item.setFloat("p_frac2", mst.getFrac2());
            call_put_mst_item.setFloat("p_frac3", mst.getFrac3());
            call_put_mst_item.setString("p_tag_perform", mst.getTagPerform());
            call_put_mst_item.setString("p_tag_status", mst.getTagStatus());

            if (mst.getDisabledDate() != null) {
                call_put_mst_item.setDate("p_disabled_date", new java.sql.Date(mst.getDisabledDate().getTime().getTime()));
            } else {
                call_put_mst_item.setDate("p_disabled_date", null);
            }
            call_put_mst_item.setInt("p_npb_fraction", mst.getNpbFraction());
            call_put_mst_item.setShort("p_gerai_id", mst.getGeraiId());
            call_put_mst_item.setByte("p_bkl", mst.getBkl());
            call_put_mst_item.setString("p_barcode4", mst.getBarcode4());
            call_put_mst_item.setString("edit_log", mst.getEditLog());
            call_put_mst_item.setString("insert_log", mst.getInsertLog());
            call_put_mst_item.setLong("version", mst.getVers());
            call_put_mst_item.setShort("item_sewa", mst.getItemSewa());
            call_put_mst_item.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_item.executeBatch();
        return clientConn;
    }

    public Connection putMstItemDiscount(Header header, List<MstItemDiscount> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item_discount = clientConn.prepareCall(ClientProcedures.put_mst_item_discount);
        for (MstItemDiscount mst : data) {
            call_put_mst_item_discount.setInt("disc_id", mst.getDiscId());
            call_put_mst_item_discount.setInt("hpp_id", mst.getHppId());
            call_put_mst_item_discount.setInt("jenis_disc", mst.getJenisDisc());
            call_put_mst_item_discount.setInt("rumus", mst.getRumus());
            if (mst.getPeriodeAwal() != null) {
                call_put_mst_item_discount.setDate("periode_awal", new java.sql.Date(mst.getPeriodeAwal().getTimeInMillis()));
            } else {
                call_put_mst_item_discount.setDate("periode_awal", null);
            }
            if (mst.getPeriodeAkhir() != null) {
                call_put_mst_item_discount.setDate("periode_akhir", new java.sql.Date(mst.getPeriodeAkhir().getTimeInMillis()));
            } else {
                call_put_mst_item_discount.setDate("periode_akhir", null);
            }
            call_put_mst_item_discount.setBigDecimal("discount", mst.getDiscount());
            call_put_mst_item_discount.setByte("is_percent", mst.getIsPercent());
            call_put_mst_item_discount.setInt("finsert", mst.getFinsert());
            call_put_mst_item_discount.setInt("fupdate", mst.getFupdate());
            call_put_mst_item_discount.setString("insert_log", mst.getInsertLog());
            call_put_mst_item_discount.setString("edit_log", mst.getEditLog());
            call_put_mst_item_discount.setLong("version", mst.getVers());
            ConnectionManager.instance().isOpen(clientConn);
            call_put_mst_item_discount.execute();
        }
        return clientConn;
    }

    public Connection putMstItemHpp(Header header, List<MstItemHpp> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item_hpp = clientConn.prepareCall(ClientProcedures.put_mst_item_hpp);
        for (MstItemHpp mst : data) {
            call_put_mst_item_hpp.setInt("hpp_id", mst.getHppId());
            call_put_mst_item_hpp.setInt("item_id", mst.getItemId());
            call_put_mst_item_hpp.setInt("vendor_id", mst.getVendorId());
            if (mst.getStartDate() != null) {
                call_put_mst_item_hpp.setDate("start_date", new java.sql.Date(mst.getStartDate().getTimeInMillis()));
            } else {
                call_put_mst_item_hpp.setDate("start_date", null);
            }
            call_put_mst_item_hpp.setInt("faction", mst.getFaction());
            call_put_mst_item_hpp.setFloat("price", mst.getPrice());
            call_put_mst_item_hpp.setFloat("discount", mst.getDiscount());
            call_put_mst_item_hpp.setFloat("tax", mst.getTax());
            call_put_mst_item_hpp.setFloat("price_cost", mst.getPriceCost());
            call_put_mst_item_hpp.setInt("pkp", mst.getPkp());
            call_put_mst_item_hpp.setInt("kondisi_barang", mst.getKondisiBarang());
            call_put_mst_item_hpp.setInt("top_buying", mst.getTopBuying());
            call_put_mst_item_hpp.setFloat("cost_aft_disc", mst.getCostAftDisc());
            call_put_mst_item_hpp.setFloat("cost_bfo_tax", mst.getCostBfoTax());
            call_put_mst_item_hpp.setFloat("cost_aft_tax", mst.getCostAftTax());
            call_put_mst_item_hpp.setString("edit_log", mst.getEditLog());
            call_put_mst_item_hpp.setString("insert_log", mst.getInsertLog());
            call_put_mst_item_hpp.setLong("version", mst.getVers());
            call_put_mst_item_hpp.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_item_hpp.executeBatch();
        return clientConn;

    }

    public Connection putMstItemLastCost(Header header, List<MstItemLastCost> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item_last_cost = clientConn.prepareCall(ClientProcedures.put_mst_item_last_cost);
        for (MstItemLastCost mst : data) {
            call_put_mst_item_last_cost.setInt("item_id", mst.getItemId());
            call_put_mst_item_last_cost.setInt("branch_id", mst.getBranchId());
            if (mst.getLastDate() != null) {
                call_put_mst_item_last_cost.setDate("last_date", new java.sql.Date(mst.getLastDate().getTimeInMillis()));
            } else {
                call_put_mst_item_last_cost.setDate("last_date", null);
            }
            call_put_mst_item_last_cost.setInt("last_vendor", mst.getLastVendor());
            call_put_mst_item_last_cost.setFloat("last_cost", mst.getLastCost());
            call_put_mst_item_last_cost.setFloat("last_cost_pcs", mst.getLastCostPcs());
            call_put_mst_item_last_cost.setBigDecimal("tax", mst.getTax());
            call_put_mst_item_last_cost.setFloat("tax_pcs", mst.getLastCostPcs());
            call_put_mst_item_last_cost.setFloat("last_cost_aft_tax", mst.getLastCostAftTax());
            call_put_mst_item_last_cost.setFloat("last_cost_aft_tax_pcs", mst.getLastCostAftTaxPcs());
            call_put_mst_item_last_cost.setFloat("avg_cost", mst.getAvgCost());
            call_put_mst_item_last_cost.setLong("version", mst.getVers());
            call_put_mst_item_last_cost.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_item_last_cost.executeBatch();
        return clientConn;
    }

    public Connection putMstItemDisplay(Header header, List<MstItemDisplay> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item_display = clientConn.prepareCall(ClientProcedures.put_mst_item_display);
        for (MstItemDisplay mst : data) {
            call_put_mst_item_display.setInt("disp_id", mst.getDispId());
            call_put_mst_item_display.setShort("tipe_rak", mst.getTipeRak());
            call_put_mst_item_display.setInt("item_id", mst.getItemId());
            call_put_mst_item_display.setString("rak", mst.getRak());
            call_put_mst_item_display.setShort("row", mst.getRow());
            call_put_mst_item_display.setShort("col", mst.getCol());
            call_put_mst_item_display.setShort("left_right", mst.getLeftRight());
            call_put_mst_item_display.setShort("up_bottom", mst.getUpBottom());
            call_put_mst_item_display.setShort("front_back", mst.getFrontBack());
            call_put_mst_item_display.setShort("max_display", mst.getMaxDisplay());

            call_put_mst_item_display.setInt("pkm", mst.getPkm());
            call_put_mst_item_display.setInt("min_order", mst.getMinOrder());
            call_put_mst_item_display.setInt("mpkm", mst.getMpkm());
            call_put_mst_item_display.setInt("n1", mst.getN1());
            call_put_mst_item_display.setInt("n2", mst.getN2());
            call_put_mst_item_display.setInt("n3", mst.getN3());

            Calendar cal1 = mst.getStartDate();
            if (cal1 != null) {
                call_put_mst_item_display.setDate("start_date", new java.sql.Date(cal1.getTimeInMillis()));
            } else {
                call_put_mst_item_display.setDate("start_date", null);
            }

            Calendar cal2 = mst.getEndDate();
            if (cal2 != null) {
                call_put_mst_item_display.setDate("end_date", new java.sql.Date(cal2.getTimeInMillis()));
            } else {
                call_put_mst_item_display.setDate("end_date", null);
            }
            call_put_mst_item_display.setString("insert_log", mst.getInsertLog());
            call_put_mst_item_display.setString("edit_log", mst.getEditLog());

            call_put_mst_item_display.setLong("version", mst.getVers());
            ConnectionManager.instance().isOpen(clientConn);
            call_put_mst_item_display.execute();
        }
        return clientConn;
    }

    public Connection putMstItemPriceTmp(Header header, List<MstItemPrice> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item_price_tmp = clientConn.prepareCall(ClientProcedures.put_mst_item_price_tmp);
        for (MstItemPrice mst : data) {
            call_put_mst_item_price_tmp.setInt("price_id", mst.getPriceId());
            call_put_mst_item_price_tmp.setInt("price_level", mst.getPriceLevel());
            call_put_mst_item_price_tmp.setInt("item_id", mst.getItemId());
            if (mst.getStartDate() != null) {
                call_put_mst_item_price_tmp.setDate("start_date", new java.sql.Date(mst.getStartDate().getTimeInMillis()));
            } else {
                call_put_mst_item_price_tmp.setDate("start_date", null);
            }
            call_put_mst_item_price_tmp.setFloat("price_cost", mst.getPriceCost());
            call_put_mst_item_price_tmp.setFloat("margin", mst.getMargin());
            call_put_mst_item_price_tmp.setFloat("price", mst.getPrice());
            call_put_mst_item_price_tmp.setFloat("markup", mst.getMarkup());
            call_put_mst_item_price_tmp.setFloat("markup_pct", mst.getMarkupPct());
            call_put_mst_item_price_tmp.setString("edit_log", mst.getEditLog());
            call_put_mst_item_price_tmp.setString("insert_log", mst.getInsertLog());
            call_put_mst_item_price_tmp.setDate("end_date", null);
            call_put_mst_item_price_tmp.setLong("version", mst.getVers());
            call_put_mst_item_price_tmp.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_item_price_tmp.executeBatch();
        return clientConn;
    }

    public Connection putMstItemPricePromo(Header header, List<MstItemPricePromo> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item_price_promo = clientConn.prepareCall(ClientProcedures.put_mst_item_price_promo);
        for (MstItemPricePromo mst : data) {
            call_put_mst_item_price_promo.setInt("price_id", mst.getPriceId());
            call_put_mst_item_price_promo.setInt("price_level", mst.getPriceLevel());
            call_put_mst_item_price_promo.setInt("item_id", mst.getItemId());
            if (mst.getStartDate() != null) {
                call_put_mst_item_price_promo.setDate("start_date", new java.sql.Date(mst.getStartDate().getTimeInMillis()));
            } else {
                call_put_mst_item_price_promo.setDate("start_date", null);
            }
            if (mst.getEndDate() != null) {
                call_put_mst_item_price_promo.setDate("end_date", new java.sql.Date(mst.getEndDate().getTimeInMillis()));
            } else {
                call_put_mst_item_price_promo.setDate("end_date", null);
            }
            call_put_mst_item_price_promo.setFloat("price_cost", mst.getPriceCost());
            call_put_mst_item_price_promo.setFloat("margin", mst.getMargin());
            call_put_mst_item_price_promo.setFloat("price", mst.getPrice());
            call_put_mst_item_price_promo.setFloat("markup", mst.getMarkup());
            call_put_mst_item_price_promo.setFloat("markup_pct", mst.getMarkupPct());
            call_put_mst_item_price_promo.setString("edit_log", mst.getEditLog());
            call_put_mst_item_price_promo.setString("insert_log", mst.getInsertLog());
            call_put_mst_item_price_promo.setLong("version", mst.getVers());
            call_put_mst_item_price_promo.setShort("branch_id", mst.getBranchId());
            call_put_mst_item_price_promo.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_item_price_promo.executeBatch();
        return clientConn;
    }

    public Connection putMstItemVendor(Header header, List<MstItemVendor> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item_vendor = clientConn.prepareCall(ClientProcedures.put_mst_item_vendor);
        for (MstItemVendor mst : data) {
            call_put_mst_item_vendor.setInt("item_vendor_id", mst.getItemVendorId());
            call_put_mst_item_vendor.setInt("item_id", mst.getItemId());
            call_put_mst_item_vendor.setInt("vendor_id", mst.getVendorId());
            call_put_mst_item_vendor.setInt("branch_id", mst.getBranchId());
            call_put_mst_item_vendor.setInt("aktif", mst.getAktif());
            if (mst.getInsertDate() != null) {
                call_put_mst_item_vendor.setDate("insert_date", new java.sql.Date(mst.getInsertDate().getTimeInMillis()));
            } else {
                call_put_mst_item_vendor.setDate("insert_date", null);
            }
            call_put_mst_item_vendor.setString("edit_log", mst.getEditLog());
            call_put_mst_item_vendor.setString("insert_log", mst.getInsertLog());
            call_put_mst_item_vendor.setLong("bkl", mst.getBkl());
            call_put_mst_item_vendor.setLong("version", mst.getVers());
            call_put_mst_item_vendor.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_item_vendor.executeBatch();
        return clientConn;
    }

    public Connection putMstModulProduct(Header header, List<MstModulProduk> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_modul_produk = clientConn.prepareCall(ClientProcedures.put_mst_modul_produk);
        for (MstModulProduk mst : data) {
            call_put_mst_modul_produk.setInt("modul_id", mst.getModulId());
            call_put_mst_modul_produk.setInt("jenis_modul", mst.getJenisModul());
            if (mst.getPeriodeAwal() != null) {
                call_put_mst_modul_produk.setDate("periode_awal", new java.sql.Date(mst.getPeriodeAwal().getTimeInMillis()));
            } else {
                call_put_mst_modul_produk.setDate("periode_awal", null);
            }
            if (mst.getPeriodeAkhir() != null) {
                call_put_mst_modul_produk.setDate("periode_akhir", new java.sql.Date(mst.getPeriodeAkhir().getTimeInMillis()));
            } else {
                call_put_mst_modul_produk.setDate("periode_akhir", null);
            }
            call_put_mst_modul_produk.setInt("jenis_produk", mst.getJenisProduk());
            call_put_mst_modul_produk.setInt("produk_id", mst.getProdukId());
            call_put_mst_modul_produk.setInt("hadiah_id", mst.getHadiahId());
            call_put_mst_modul_produk.setFloat("batas", mst.getBatas());
            call_put_mst_modul_produk.setFloat("nilai_hadiah", mst.getNilaiHadiah());
            call_put_mst_modul_produk.setFloat("batas_tambahan", mst.getBatasTambahan());
            call_put_mst_modul_produk.setFloat("nilai_tambahan", mst.getNilaiTambahan());
            call_put_mst_modul_produk.setInt("finsert", mst.getFinsert());
            call_put_mst_modul_produk.setInt("fupdate", mst.getFupdate());
            call_put_mst_modul_produk.setString("insert_log", mst.getInsertLog());
            call_put_mst_modul_produk.setString("edit_log", mst.getEditLog());
            call_put_mst_modul_produk.setInt("tipe", mst.getTipe());
            call_put_mst_modul_produk.setString("calc_tipe", mst.getCalcTipe());
            call_put_mst_modul_produk.setInt("kelipatan", mst.getKelipatan());
            call_put_mst_modul_produk.setFloat("batas_max", mst.getBatasMax());
            call_put_mst_modul_produk.setString("note", mst.getNote());
            call_put_mst_modul_produk.setLong("version", mst.getVers());
            call_put_mst_modul_produk.setFloat("nilai_anggota", mst.getNilaiAnggota());
            call_put_mst_modul_produk.setInt("card_id", mst.getCardId());
            call_put_mst_modul_produk.setFloat("batas_card", mst.getBatasCard());
            call_put_mst_modul_produk.setFloat("nilai_card", mst.getNilaiCard());
            call_put_mst_modul_produk.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_modul_produk.executeBatch();
        return clientConn;

    }

    public Connection putMstPointMapping(Header header, List<MstPointMaping> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_point_mapping = clientConn.prepareCall(ClientProcedures.put_mst_point_mapping);
        for (MstPointMaping mst : data) {
            call_put_mst_point_mapping.setInt("code_id", mst.getCodeId());
            call_put_mst_point_mapping.setInt("root_id", mst.getRootId());
            call_put_mst_point_mapping.setLong("version", mst.getVers());
            call_put_mst_point_mapping.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_point_mapping.executeBatch();
        return clientConn;
    }

    public Connection putMstRakSewa(Header header, List<MstRakSewa> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_rak_sewa = clientConn.prepareCall(ClientProcedures.put_mst_rak_sewa);
        for (MstRakSewa mst : data) {
            call_put_mst_rak_sewa.setInt("rak_id", mst.getRakId());
            call_put_mst_rak_sewa.setString("rak_kode", mst.getRakKode());
            call_put_mst_rak_sewa.setString("rak_nama", mst.getRakNama());
            call_put_mst_rak_sewa.setString("rak_type", mst.getRakType());
            call_put_mst_rak_sewa.setString("insert_log", mst.getInsertLog());
            call_put_mst_rak_sewa.setString("edit_log", mst.getEditLog());
            call_put_mst_rak_sewa.setLong("version", mst.getVers());
            call_put_mst_rak_sewa.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_rak_sewa.executeBatch();
        return clientConn;
    }

    public Connection putMstRelation(Header header, List<MstRelation> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_relation = clientConn.prepareCall(ClientProcedures.put_mst_relation);
        for (MstRelation mst : data) {
            call_put_mst_relation.setInt("relation_id", mst.getRelationId());
            call_put_mst_relation.setString("relation_code", mst.getRelationCode());
            call_put_mst_relation.setString("relation_name", mst.getRelationName());
            call_put_mst_relation.setString("address", mst.getAddress());
            call_put_mst_relation.setString("city", mst.getCity());
            call_put_mst_relation.setString("post_code", mst.getPostCode());
            call_put_mst_relation.setString("phone", mst.getPhone());
            call_put_mst_relation.setString("fax", mst.getFax());
            call_put_mst_relation.setString("contact_person", mst.getContactPerson());
            call_put_mst_relation.setString("npwp", mst.getNpwp());
            call_put_mst_relation.setString("npwp_name", mst.getNpwpName());
            call_put_mst_relation.setString("npwp_address", mst.getNpwpAddress());
            call_put_mst_relation.setString("skp", mst.getSkp());
            if (mst.getSkpDate() != null) {
                call_put_mst_relation.setDate("skp_date", new java.sql.Date(mst.getSkpDate().getTimeInMillis()));
            } else {
                call_put_mst_relation.setDate("skp_date", null);
            }
            call_put_mst_relation.setString("jadwal_order", mst.getJadwalOrder());
            call_put_mst_relation.setByte("lead_time", mst.getLeadTime());
            call_put_mst_relation.setInt("term_of_payment", mst.getTermOfPayment());
            call_put_mst_relation.setInt("open_top", mst.getOpenTop());
            call_put_mst_relation.setInt("kondisi_barang", mst.getKondisiBarang());
            call_put_mst_relation.setByte("jenis_pajak", mst.getJenisPajak());
            call_put_mst_relation.setString("email", mst.getEmail());
            call_put_mst_relation.setString("website", mst.getWebsite());
            call_put_mst_relation.setFloat("current_debt", mst.getCurrentDebt());
            call_put_mst_relation.setInt("hold_order", mst.getHoldOrder());
            call_put_mst_relation.setString("ac_name", mst.getAcName());
            call_put_mst_relation.setString("bank_name", mst.getBankName());
            call_put_mst_relation.setString("acc_id", mst.getAccId());
            if (mst.getDisabledDate() != null) {
                call_put_mst_relation.setDate("disabled_date", new java.sql.Date(mst.getDisabledDate().getTimeInMillis()));
            } else {
                call_put_mst_relation.setDate("disabled_date", null);
            }
            call_put_mst_relation.setString("edit_log", mst.getEditLog());
            call_put_mst_relation.setString("insert_log", mst.getInsertLog());
            call_put_mst_relation.setLong("version", mst.getVers());
            call_put_mst_relation.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_relation.executeBatch();
        return clientConn;
    }

    public Connection putMstTenant(Header header, List<MstTenant> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_tenant = clientConn.prepareCall(ClientProcedures.put_mst_tenant);
        for (MstTenant mst : data) {
            call_put_mst_tenant.setInt("tenant_id", mst.getTenantId());
            call_put_mst_tenant.setString("tenant_code", mst.getTenantCode());
            call_put_mst_tenant.setString("tenant_name", mst.getTenantName());
            call_put_mst_tenant.setString("address", mst.getAddress());
            call_put_mst_tenant.setString("address_last", mst.getAddressLast());
            call_put_mst_tenant.setString("ktp", mst.getKtp());
            call_put_mst_tenant.setString("phone", mst.getPhone());
            call_put_mst_tenant.setString("hp", mst.getHp());
            call_put_mst_tenant.setString("usaha_jenis", mst.getUsahaJenis());
            call_put_mst_tenant.setFloat("usaha_hasil_harian", mst.getUsahaHasilHarian());
            call_put_mst_tenant.setInt("usaha_jml_pembeli", mst.getUsahaJmlPembeli());
            call_put_mst_tenant.setInt("peralatan_dimensi", mst.getPeralatanDimensi());
            call_put_mst_tenant.setString("peralatan_other", mst.getPeralatanOther());
            call_put_mst_tenant.setInt("peralatan_panjang", mst.getPeralatanPanjang());
            call_put_mst_tenant.setInt("peralatan_lebar", mst.getPeralatanLebar());
            call_put_mst_tenant.setInt("peralatan_tinggi", mst.getPeralatanTinggi());
            call_put_mst_tenant.setFloat("laba_perhari", mst.getLabaPerhari());
            call_put_mst_tenant.setFloat("biaya_bahan_pokok", mst.getBiayaBahanPokok());
            call_put_mst_tenant.setFloat("biaya_operasional", mst.getBiayaOperasional());
            call_put_mst_tenant.setFloat("biaya_lain", mst.getBiayaLain());
            call_put_mst_tenant.setFloat("pendatapan_bulan", mst.getPendatapanBulan());
            if (mst.getDisabledDate() != null) {
                call_put_mst_tenant.setDate("disabled_date", new java.sql.Date(mst.getDisabledDate().getTimeInMillis()));
            } else {
                call_put_mst_tenant.setDate("disabled_date", null);
            }
            call_put_mst_tenant.setString("insert_log", mst.getInsertLog());
            call_put_mst_tenant.setString("edit_log", mst.getEditLog());
            call_put_mst_tenant.setString("npwp", mst.getNpwp());
            call_put_mst_tenant.setInt("is_first", mst.getIsFirst());
            call_put_mst_tenant.setLong("version", mst.getVers());
            call_put_mst_tenant.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_tenant.executeBatch();
        return clientConn;
    }

    public Connection putMstVoucher(Header header, List<MstVoucher> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_voucher = clientConn.prepareCall(ClientProcedures.put_mst_voucher);
        for (MstVoucher mst : data) {
            call_put_mst_voucher.setInt("voucher_id", mst.getVoucherId());
            call_put_mst_voucher.setString("voucher_code", mst.getVoucherCode());
            call_put_mst_voucher.setFloat("amount", mst.getAmount());

            if (mst.getExpiredDate() != null) {
                call_put_mst_voucher.setDate("expire_date", new java.sql.Date(mst.getExpiredDate().getTimeInMillis()));
            } else {
                call_put_mst_voucher.setDate("expire_date", null);
            }

            if (mst.getDisabledDate() != null) {
                call_put_mst_voucher.setDate("disabled_date", new java.sql.Date(mst.getDisabledDate().getTimeInMillis()));
            } else {
                call_put_mst_voucher.setDate("disabled_date", null);
            }

            call_put_mst_voucher.setByte("finsert", (byte) 0);
            call_put_mst_voucher.setByte("fupdate", (byte) 0);
            call_put_mst_voucher.setString("notes", mst.getNotes());
            call_put_mst_voucher.setInt("used", mst.getUsed());
            call_put_mst_voucher.setInt("branch_id", mst.getBranchId());

            if (mst.getUsedDate() != null) {
                call_put_mst_voucher.setDate("used_date", new java.sql.Date(mst.getUsedDate().getTimeInMillis()));
            } else {
                call_put_mst_voucher.setDate("used_date", null);
            }
            call_put_mst_voucher.setString("insert_log", mst.getInsertLog());
            call_put_mst_voucher.setString("edit_log", mst.getEditLog());
            call_put_mst_voucher.setLong("version", mst.getVers());
            call_put_mst_voucher.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_voucher.executeBatch();
        return clientConn;
    }

    public Connection putMstBranch(Header header, List<MstBranch> data, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_branch = clientConn.prepareCall(ClientProcedures.put_mst_branch);
        for (MstBranch mst : data) {
            call_put_mst_branch.setShort("branch_id", mst.getBranchId());
            call_put_mst_branch.setString("branch_name", mst.getBranchName());
            call_put_mst_branch.setString("nickname", mst.getNickname());
            call_put_mst_branch.setString("address", mst.getAddress());
            call_put_mst_branch.setString("address2", mst.getAddress2());
            call_put_mst_branch.setString("city", mst.getCity());
            call_put_mst_branch.setString("phone", mst.getPhone());
            call_put_mst_branch.setString("phone2", mst.getPhone2());
            call_put_mst_branch.setString("fax", mst.getFax());
            call_put_mst_branch.setString("post_code", mst.getPostCode());
            call_put_mst_branch.setString("branch_code", mst.getBranchCode());
            call_put_mst_branch.setString("deliver_address", mst.getDeliverAddress());
            call_put_mst_branch.setString("deliver_name", mst.getDeliverName());
            Calendar cal1 = null;
            if (mst.getDisabledDate() != null) {
                call_put_mst_branch.setDate("disabled_date", new java.sql.Date(mst.getDisabledDate().getTimeInMillis()));
            } else {
                call_put_mst_branch.setDate("disabled_date", null);
            }
            call_put_mst_branch.setString("email", mst.getEmail());
            call_put_mst_branch.setShort("area_id", mst.getAreaId());
            call_put_mst_branch.setShort("tipe_rak", mst.getTipeRak());
            call_put_mst_branch.setByte("online", mst.getOnline());
            call_put_mst_branch.setString("insert_log", mst.getInsertLog());
            call_put_mst_branch.setString("edit_log", mst.getEditLog());
            call_put_mst_branch.addBatch();
        }
        ConnectionManager.instance().isOpen(clientConn);
        call_put_mst_branch.executeBatch();
        return clientConn;
    }

    public Connection putMstPkmGerai(Header header, List<MstPkmGerai> list, Connection clientConn) throws SQLException {
        LOG.debug("<PUT>");
        call_put_mst_item_display = clientConn.prepareCall(ClientProcedures.put_mst_pkm_gerai);
        for (MstPkmGerai mst : list) {
            
            call_put_mst_item_display.setInt("item_id", mst.getItemId());
            call_put_mst_item_display.setFloat("pkm", mst.getPkm());
            call_put_mst_item_display.setFloat("dsi", mst.getDsi());
            call_put_mst_branch.addBatch();
        }
        call_put_mst_item_display.executeBatch();
        return clientConn;
    }
}
